package xyz.xtt.asset.mq;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import com.google.common.primitives.Longs;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.entity.TblAsset;
import xyz.xtt.asset.entity.TblAssetGroup;
import xyz.xtt.asset.entity.TblAssetTransaction;
import xyz.xtt.asset.enums.OrderTypeEnum;
import xyz.xtt.asset.service.ITblAssetGroupService;
import xyz.xtt.asset.service.ITblAssetService;
import xyz.xtt.asset.service.ITblAssetTransactionService;
import xyz.xtt.asset.service.IThirdService;
import xyz.xtt.asset.vo.CreateGorupResult;
import xyz.xtt.pay.client.response.TblPayOrderDto;

/**
 * 支付后建群
 *
 * @author dale
 * @since 2024/12/30
 **/
@RefreshScope
@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.pay.order.status.change.topic:nft_pay_order_status_change}", selectorExpression = "PERSONAL_MINT:PAY_SUCCESS", consumerGroup = "${rocketmq.consumer.group.mintafter}")
public class PayMintAfterConsumer implements RocketMQListener<String> {
	@Autowired
	private IThirdService thirdService;
	@Autowired
	private ITblAssetTransactionService iTblAssetTransactionService;
	@Autowired
	private ITblAssetService iTblAssetService;
	@Autowired
	private ITblAssetGroupService iTblAssetGroupService;

	@Override
	public void onMessage(String message) {
		log.info("PayMintAfterConsumer nft_pay_order_status_change  message: {}", message);
		if (StringUtils.isEmpty(message)) {
			return;
		}
		if (!JSONUtil.isTypeJSON(message)) {
			log.warn("PayMintAfterConsumer nft_pay_order_status_change Received message: {} is not json", message);
			return;
		}
		try {
			TblPayOrderDto payOrderDto = JSONUtil.toBean(message, TblPayOrderDto.class);
			if (Objects.isNull(payOrderDto)) {
				log.warn("PayMintAfterConsumer nft_pay_order_status_change JSONUtil.toBean is null. message:{}", message);
				return;
			}
			TblAssetTransaction transaction = iTblAssetTransactionService.getById(payOrderDto.getBusinessRefId());
			OrderTypeEnum orderType = OrderTypeEnum.queryConvert(transaction.getOrderType());
			if (Objects.isNull(orderType)) {
				log.info("PayMintAfterConsumer nft_pay_order_status_change orderType is null msg:{}", message);
				return;
			}
			if (!Objects.equals(orderType, OrderTypeEnum.PERSONAL_MINT)) {
				log.info("PayMintAfterConsumer nft_pay_order_status_change orderType:{} not equal PERSONAL_MINT msg:{}", orderType, message);
				return;
			}
			TblAsset asset = iTblAssetService.info(transaction.getGoodsId());
			if (Objects.isNull(asset)) {
				log.info("PayMintAfterConsumer nft_pay_order_status_change asset is null msg:{}", message);
				return;
			}
			TblAssetGroup queryAssetGroup = iTblAssetGroupService.queryAssetGroup(asset.getId());
			if (Objects.nonNull(queryAssetGroup)) {
				log.info("PayMintAfterConsumer nft_pay_order_status_change group created msg:{}", message);
				return;
			}
			CreateGorupResult createGorup = thirdService.createGorup(asset.getId(), Longs.tryParse(payOrderDto.getBusinessRefId()), payOrderDto.getAppUserId(), asset.getAssetName());
			log.info("PayMintAfterConsumer nft_pay_order_status_change createGorup:{} message: {}", JSONUtil.toJsonStr(createGorup), message);
		} catch (Exception e) {
			log.info("PayMintAfterConsumer consume failed. message:{} ex:{}", message, e.getMessage(), e);
			throw new RuntimeException("PayMintAfterConsumer Consumer Message exception", e);
		}
	}
}
